import React from 'react';
import { connect } from 'dva';

import { Table, Button,Form, DatePicker } from 'antd';


import moment from 'moment';

const FormItem = Form.Item;

@connect(({ accountBalance }) => ({ accountBalance }))
class List extends React.Component {
  state = {
  }

  treeview = [
      { title: '科目代码', dataIndex: 'account_code', },
      { title: '科目名称', dataIndex: 'account_name', },
      { title: '明细代码', dataIndex: 'sub_account_code', },
      { title: '明细名称', dataIndex: 'sub_account_name', },
      { title: '日期', dataIndex: 'date', },
      { title: '凭证字号', dataIndex: 'name', },
      { title: '摘要', dataIndex: 'label', },
      { title: '借方', dataIndex: 'debit', },
      { title: '贷方', dataIndex: 'credit', },
      { title: '余额', dataIndex: 'balance', },
  ]

  async componentDidMount() {
  }


  handleSubmit = e => {
    const { dispatch, form } = this.props;
    e.preventDefault();

    form.validateFieldsAndScroll( async (err, values) => {

      if (!err) {

        const { date } = values;

        const date2 = `${date.year()}-${date.month() + 1}-${date.date()}`

        await dispatch({
          type: 'accountBalance/get_detail_ledger',
          payload: { date: date2 },
        });

      }
    });
  };


  render() {
    const { accountBalance: { detailLedgerData } } = this.props;
    const { form: { getFieldDecorator }  } = this.props;

    return (
      <div>


        <Form  onSubmit={this.handleSubmit}>
            <FormItem style={{ marginTop: 32 }}>
              <Button type="primary" htmlType="submit" >查询
              </Button>
            </FormItem>

            <FormItem label="日期">
              {getFieldDecorator('date', {
                rules: [{ required: true }],
                initialValue:  moment()
               // initialValue: moment().format('YYYY-MM-DD')
               // initialValue: moment(record.date, 'YYYY-MM-DD'),
              })(<DatePicker />)}
            </FormItem>
        </Form>

        <Table columns={this.treeview} dataSource={detailLedgerData} rowKey="id" />

      </div>
    );
  }
}

export default Form.create()(List);





